package org.zfes.snowier.cloud.zbss.pmp.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.zfes.snowier.cloud.zbss.pmp.dto.PmpProjectVersionCreateDto;
import org.zfes.snowier.core.data.ComboboVo;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpProjectService;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpProjectVersionService;
import org.zfes.snowier.cloud.zbss.pmp.dao.PmpProjectVersionMapper;
import org.zfes.snowier.cloud.zbss.pmp.model.PmpProjectVersion;
import org.zfes.snowier.core.util.ZDateUtil;

import com.google.common.collect.Lists;

import java.util.stream.Stream;
@Service
public class PmpProjectVersionServiceImpl implements IPmpProjectVersionService{
 	@Autowired
	private PmpProjectVersionMapper pmpProjectVersionMapper;
 	@Autowired
	private IPmpProjectService pmpProjectService;
 	
 	@Transactional(rollbackFor = Exception.class)
	@Override
	public void createPmpProjectVersion(PmpProjectVersionCreateDto versionCreateDto) {
		ZBeanUtil.setUpdatedInfo(versionCreateDto);
		ZBeanUtil.validateBean(versionCreateDto);
		PmpProjectVersion pmpProjectVersion = new PmpProjectVersion();
		ZBeanUtil.copy(versionCreateDto,pmpProjectVersion,true);
		
		Long vid=createPmpProjectVersion( pmpProjectVersion);
		if(Byte.valueOf("1").equals(versionCreateDto.getUseThis())) {
			pmpProjectService.updatePmpProjectVersion(versionCreateDto.getProjectId(), vid);
		}
	}
 	
 	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public Long createPmpProjectVersion(PmpProjectVersion pmpProjectVersion) {
		ZBeanUtil.setCreateInfo(pmpProjectVersion);
		ZBeanUtil.validateBean(pmpProjectVersion);
		pmpProjectVersion.setCreatetime(ZDateUtil.newDate());
		pmpProjectVersion.setUpdatetime(ZDateUtil.newDate());

		pmpProjectVersionMapper.insertSelective(pmpProjectVersion);
		return pmpProjectVersion.getId();
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<PmpProjectVersion> loadPmpProjectVersionById(Long id) {
		if(id!=null){
			return Optional.ofNullable(pmpProjectVersionMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updatePmpProjectVersion(PmpProjectVersionCreateDto versionUpdateDto) {
		ZAssert.notNull(versionUpdateDto.getId()	, "ID参数错误");
		ZBeanUtil.validateBean(versionUpdateDto);
		PmpProjectVersion pmpProjectVersion=new PmpProjectVersion();
		ZBeanUtil.copy(versionUpdateDto, pmpProjectVersion, true);
		updatePmpProjectVersion( pmpProjectVersion);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updatePmpProjectVersion(PmpProjectVersion pmpProjectVersion) {
		ZBeanUtil.setUpdatedInfo(pmpProjectVersion);
		ZBeanUtil.validateBean(pmpProjectVersion);
		pmpProjectVersionMapper.updateByPrimaryKeySelective(pmpProjectVersion);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deletePmpProjectVersion(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		pmpProjectVersionMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setPmpProjectVersionStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		pmpProjectVersionMapper.updateStatus( id,  status) ;
	}

	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<ComboboVo> loadVersionComoboVo(Long projectId,Boolean firstEmpty){
		if(projectId==null) {
			return Lists.newArrayList();
		}
		List<ComboboVo> voList=pmpProjectVersionMapper.selectComboboVo(projectId,Byte.valueOf("1"));
		if(firstEmpty) {
			voList.add(0, ComboboVo.newEmptyFirst());
		}
		return voList;
	}
	

	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadPmpProjectVersionDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		if(!pm.getLong("projectId").isPresent()) {
			return DataSet.emptyDS();
		}
		pm.getObj("version").ifPresent(v->pm.updateParam("version", "%"+v+"%"));
		DataSet ds=DataSet.newDS2(pmpProjectVersionMapper.selectListCount(pm), pmpProjectVersionMapper.selectMapListPage(pm));
		return ds;
	}
	
}